package server.service.patient;

import java.util.List;
import java.util.logging.Logger;

import org.json.JSONArray;
import org.json.JSONObject;

import domain.exception.ValidationException;
import domain.patient.Patient;
import domain.session.Response;

public class PatientAction {
	
	private static final Logger LOGGER = Logger.getLogger(PatientAction.class.getName());
	
	private PatientModel patientModel;
	
	public PatientAction() {
		patientModel = new PatientModel();
	}
	
	public Response getPatient(final JSONObject requestJson) {
		try {
			Integer userId = requestJson.getInt("patientId");
			Patient patient = patientModel.getPatient(userId);
			Response response = new Response(true, patient.getJSON());
			return response;
		} catch (NumberFormatException nfe) {
			LOGGER.warning("Number Format Exception: " + nfe);
			return null;
		}
	}
	
	public Response findPatients(final JSONObject requestJson) {
		String query = requestJson.getString("query");
		List<Patient> patients = patientModel.findPatients(query);
		JSONArray patientArray = new JSONArray();
		for (Patient patient : patients) {
			patientArray.put(patient.getJSON());
		}
		JSONObject responseJson = new JSONObject();
		responseJson.put("patients", patientArray);
		Response response = new Response(true, responseJson);
		return response;
	}
	
	public Response getAllPatients(final JSONObject requestJson) {
		List<Patient> allPatients = patientModel.getAllPatients();
		JSONArray allPatientArray = new JSONArray();
		for (Patient patient : allPatients) {
			allPatientArray.put(patient.getJSON());
		}
		JSONObject responseJson = new JSONObject();
		responseJson.put("patients", allPatientArray);
		Response response = new Response(true, responseJson);
		return response;
	}
	
	public Response modifyDOB(final JSONObject requestJson) {
		Integer userId = requestJson.getInt("userId");
		String date = requestJson.getString("date");
		Response response = new Response();
		try {
			patientModel.modifyDOB(userId, date);
		} catch (ValidationException e) {
			JSONObject responseJson = new JSONObject();
			responseJson.put("msg", e.getExceptionText());
			response.setJSON(responseJson);
			response.setSuccessful(false);
			return response;
		}
		response.setSuccessful(true);
		return response;
	}
	
	public Response modifyAllergies(final JSONObject requestJson) {
		Integer id = requestJson.getInt("userId");
		String allergies = requestJson.getString("allergies");
		patientModel.modifyAllergies(id, allergies);
		Response response = new Response();
		response.setSuccessful(true);
		return response;
	}
	
	public Response modifyInsurance(final JSONObject requestJson) {
		Integer insuranceId = requestJson.getInt("userId");
		String insurance = requestJson.getString("insurance");
		patientModel.modifyInsurance(insuranceId, insurance);
		Response response = new Response();
		response.setSuccessful(true);
		return response;
	}
	
	public Response modifyContact(final JSONObject requestJson) {
		Integer contactId = requestJson.getInt("userId");
		String address = requestJson.getString("address");
		String email = requestJson.getString("email");
		Response response = new Response();
		try {
			patientModel.modifyContact(contactId, address, email);
		} catch (ValidationException e) {
			JSONObject responseJson = new JSONObject();
			responseJson.put("msg", e.getExceptionText());
			response.setJSON(responseJson);
			response.setSuccessful(false);
			return response;
		}
		response.setSuccessful(true);
		return response;
	}

}
